package stats;

import graphics.*;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;

import core.Mode;

public class Statistics {

	static ArrayList<Double> bestInd = new ArrayList<Double>();
	static ArrayList<Double> bestTeam = new ArrayList<Double>();
	static ArrayList<Double> midInd = new ArrayList<Double>();
	static ArrayList<Double> mean = new ArrayList<Double>();
	
	static int count = 0;
	
	
	
	static int currCount;
	static double currSum;
	static double currBestInd;
	static double currBestTeam;
	static double currMidInd;
	
	
//	static ArrayList<Double> maxResult = new ArrayList<Double>();
//	static ArrayList<Double> maxCost = new ArrayList<Double>();
//	static ArrayList<Double> averageAll = new ArrayList<Double>();

//	final static Boolean drawMonitor = new Boolean(true);

	public static void advanceGenerations() {

//		if (count < 50 || ((count & 7) == 0)) {
//			System.err.printf("%d: %.15f max, %.6f max cost, %.6f average\n",count,currBestInd,currBestTeam,currSum / currCount);
//			System.err.flush();
//		}
		// if (Integer.bitCount(genCount) <= 1) {
		synchronized (GraficPainter.drawMonitor) {
			count++;
			bestInd.add(currBestInd);
			bestTeam.add(currBestTeam);
			midInd.add(currMidInd);
			mean.add(currSum / currCount);
		}

		reset();
		if (count < 50 || ((count & 15) == 0))
			MainApp.update();
	}
	
	private static void reset() {
		currCount = 0;
		currSum = 0;

		currBestInd = Double.NEGATIVE_INFINITY;
		currBestTeam = Double.NEGATIVE_INFINITY;	
		currMidInd = Double.NEGATIVE_INFINITY;	
	}




	static {
		reset();
	}
	
	public static void addTeamResult(double result) {
		currBestTeam = Math.max(currBestTeam, result);
		currCount++;
		currSum += result;
	}

	
	private static double[] help = new double[Mode.mode.popSize];
	
	public static void addTotalCosts(double[] costs, double multiply) {
//		System.err.println(multiply + " " + Arrays.toString(costs));
		System.arraycopy(costs, 0, help, 0, Mode.mode.popSize);
		Arrays.sort(help);
		currBestInd = help[help.length-1];
		currMidInd = multiply * help[Mode.mode.popSize- (Mode.mode.popSize>>2)];
	}
	
	public static void finish() {
//		System.out.println(maxResult);
//		System.out.println(averageAll);
	}

	public static void paint(Graphics2D g2, Point2D start, Point2D dim) {
		GraficPainter.drawGrafic(g2, start, dim, true, false, bestInd, bestTeam, mean, midInd);
		
		g2.setColor(Color.BLACK);
		g2.setFont(new Font("Verdana",Font.BOLD, 12));
		g2.drawString(Mode.mode.explanation,(int) (start.getX()+.1 * dim.getX()), (int)start.getY()-10);
		
		g2.setFont(new Font("Arial",Font.PLAIN, 12));
		g2.setColor(GraficPainter.colors[0]);
		g2.setStroke(new BasicStroke(5f));
		g2.drawLine((int)(.1*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f), (int)(.13*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f));
		g2.drawString("BestInd", (int)(.15*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+40f));
		g2.setColor(GraficPainter.colors[1]);
		g2.drawLine((int)(.3*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f), (int)(.33*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f));
		g2.drawString("BestTeam", (int)(.35*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+40f));
		g2.setColor(GraficPainter.colors[2]);
		g2.drawLine((int)(.53*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f), (int)(.56*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f));
		g2.drawString("Mean", (int)(.58*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+40f));
		g2.setColor(GraficPainter.colors[3]);
		g2.drawLine((int)(.7*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f), (int)(.73*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+35f));
		g2.drawString("MidInd", (int)(.75*dim.getX()+start.getX()), (int)(start.getY()+dim.getY()+40f));
		g2.setStroke(new BasicStroke(1f));
	}
	
	public static ArrayList<Double>[] getLists() {
		return (ArrayList<Double>[]) new ArrayList<?>[] {bestInd, bestTeam, mean, midInd};
	}
	
}
